package com.cesc.demo.application.service;

import com.cesc.demo.domain.product.IDomainService;
import com.cesc.demo.domain.product.PageResult;
import com.cesc.demo.domain.product.entity.Product;
import com.cesc.demo.domain.product.service.IProductDomainService;
import com.cesc.demo.domain.product.service.ProductDomainService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;

/***
 * 应用服务层，调用领域服务层
 */
@Service
public class ProductAppService {
    @Autowired
    private IProductDomainService productDomainService;
    private static  final Logger logger= LoggerFactory.getLogger(ProductAppService.class);


    @Transactional
    public Mono<Product> saveByRespository(Product product) {
        return   this.productDomainService.saveByRespository(product);
    }

    @Transactional
    public Mono<Product> saveByRespositoryWithRollBack(Product product) {
        return   this.productDomainService.saveByRespositoryWithRollBack(product);
    }


    @Transactional
    public Mono<Void> deletByIdByRespository(Long id) {
        return  this.productDomainService.deletByIdByRespository(id).onErrorMap(error->{
            logger.error("删除产品失败",error);
            return error;
        });
    }

    public Mono<Product> findByRespository(Long id) {
        return   this.productDomainService.findByIdByRespository(id);
    }


    @Transactional
    public Mono<Product> saveByAccessApi(Product product) {
        return   this.productDomainService.saveByAccessApi(product);
    }

    @Transactional
    public Mono<Product> updateByAccessApi(Product product) {
        return   this.productDomainService.updateByAccessApi(product);
    }

    @Transactional
    public Mono<Integer> deleteByAccessApi(Product product) {
        return   this.productDomainService.deleteByAccessApi(product);
    }


    public Mono<PageResult<Product>> findByAccessApi(Product product,int pageSize,int page) {
        return   this.productDomainService.findByAccessApi(product,pageSize,page);
    }

    public Mono<List<Product>> findProductByProductNameEquals(String productName) {
        return this.productDomainService.findProductByProductNameEquals(productName).collectList();
    }

}
